package lab1.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import lab1.dao.RolesDAO;
import lab1.dao.UserDAO;
import lab1.vo.RoleVO;
import lab1.vo.UserVO;
import lab1.vo.ValueObject;

/**
 * 
 * @author Arty
 * 
 */

public class Application {
	public static final String name = "Lab 1";
	public static final String version = "0";
	private UserVO user = null;
	
	public Application() {

	}

	public void init() {
		System.out.println(name + ", version " + version);
		try {
			setLogInUser();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setLogInUser() throws IOException {
		String log = null;
		String pas = null;
		UserVO user = new UserVO();
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("login :");
		log = bf.readLine();

		System.out.print("password:");

		pas = bf.readLine();

		UserDAO usDao = new UserDAO();
		user.setLogin(log);
		user.setPassword(pas);
		user = (UserVO) usDao.retrieve(user);
		if (user != null) {
			this.user = user;
			setRoles(getLogInUser());
			// executeAction(user);
		} else {
			System.out.println("Authentication failed");
			setLogInUser();
		}

	}
	
	public UserVO getLogInUser(){
		return this.user;
	}

	public void logOutUser() throws Throwable {
		System.out.println("Va multumim, aplicatie STOP!");
		this.finalize();
	}

	public void setRoles(UserVO us) {
		ArrayList<ValueObject> arrRol = new ArrayList<ValueObject>();
		RolesDAO rol = new RolesDAO();
		arrRol = rol.retrieve(us);

		if (!arrRol.isEmpty()) {
			for (int i = 0; i < arrRol.size(); i++) {
				RoleVO rolVO = new RoleVO();
				rolVO = (RoleVO) arrRol.get(i);
				System.out.println(rolVO.getId() + " - "
						+ rolVO.getDescription());
			}
			executeRole(arrRol, us);
		} else {
			System.out.println("Sorry no roles for you!");

		}
	}

	private void executeRole(ArrayList<ValueObject> arrRol, UserVO us) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String action = null;
		boolean check = false;
		try {
			action = bf.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (isNumber(action)) {
			for (int i = 0; i < arrRol.size(); i++) {
				RoleVO rolVO = new RoleVO();
				rolVO = (RoleVO) arrRol.get(i);
				if (Integer.parseInt(action) == rolVO.getId()) {
					check = true;
				}
			}
		}// End - if

		if (!check) {
			System.out.println("Introduce-ti va rog un numar valid!");
			executeRole(arrRol, us);
		} else {
			// Structura nu este generalizata!
			switch (Integer.parseInt(action)) {
			case 1:// update
				updateUser(us);
				break;
			case 2:// delete
				deleteUser(us);
				break;
			case 3:// insert/create
				insertUser();
				break;
			case 4:// detail
				showDetail(us);
				break;
			case 5:// logOut
				try {
					logOutUser();
				} catch (Throwable e) {
					e.printStackTrace();
				}
				break;
			default:
			}// End - switch
		}// End if/else
	}// End executeRole(ArrayList<ValueObject> arrRol)

	private boolean isNumber(String i) {
		try {
			Integer.parseInt(i, 10);
			return true;
		} catch (Exception e) {
			return false;
		}
	}// End - isNUmber(String i)

	public void updateUser(UserVO us) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		UserVO modUs = new UserVO();
		UserDAO usDAO = new UserDAO();
		modUs = us;
		System.out.println("Modificarea datelor personale, utilizator: "
				+ us.getLogin());
		System.out.println("parola:");
		try {
			modUs.setPassword(bf.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (usDAO.update(modUs)) {
			System.out.println("Datele modificate cu succes.");
		} else {
			System.out.println("Eroare modificare date!");
		}
		setRoles(getLogInUser());
	}

	public void deleteUser(UserVO us) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		Integer id = null;
		UserDAO usDAO = new UserDAO();
		System.out.println("Introduceti id user ce va fi eliminat:");

		try {
			id = Integer.parseInt(bf.readLine());
			if (usDAO.delete(id)) {
				System.out.println("User cu id " + id + " eliminat cu succes!");
			} else {
				System.out.println("Eroare eliminare user!");
			}
		} catch (NumberFormatException e) {
			System.out.println("Id not valid!");
			deleteUser(us);
		} catch (IOException e) {
			System.out.println("Eroare eliminare user!");
		}
		setRoles(getLogInUser());
	}

	public void insertUser() {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Insert new user:");
		UserVO us = new UserVO();
		UserDAO usDAO = new UserDAO();
		System.out.print("Nume utilizator:");
		try {
			us.setName(bf.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.print("login:");
		try {
			us.setLogin(bf.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.print("pasw:");
		try {
			us.setPassword(bf.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.print("1 - Admin / 2 - User:");
		try {
			us.setGroupId(Integer.parseInt(bf.readLine()));
		} catch (Exception e) {
			us.setGroupId(2);
			System.out.println("Eroare setare group, default setat User!");
		}

		usDAO.create(us);
		
		System.out.println("================================================");
		setRoles(getLogInUser());
	}

	public void showDetail(UserVO us) {
		System.out.println("================================================");
		System.out.println(" Personal info of user " + us.getName());
		System.out.println(" ID:" + us.getId());
		System.out.println(" LogIn:" + us.getLogin());
		System.out.println(" Passw:" + us.getPassword());
		System.out.println("================================================");

		setRoles(getLogInUser());
	}// End - showDetail(UserVO us)

}
